package com.aier.classes.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.classes.entity.Homework;

public class HomeworkRowMapper implements RowMapper<Homework> {

    @SuppressWarnings("unused")
    private int type;

    public static final int AND_MESSAGE = 1;

    public HomeworkRowMapper() {
    }

    public HomeworkRowMapper(int type) {
        this.type = type;
    }

    @Override
    public Homework mapRow(ResultSet rs, int rowNum) throws SQLException {
        Homework hw = parseClass(new Homework(), rs);
        switch (type) {
            case AND_MESSAGE:
                hw.setSuccessStr(rs.getString("successStr"));
                hw.setFailStr(rs.getString("failStr"));
                break;
        }
        return hw;
    }

    public static Homework parseClass(Homework cl, ResultSet rs)
        throws SQLException {
        return parseClass(cl, rs, null);
    }

    public static Homework parseClass(Homework cl, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            cl.setHid(rs.getLong(su + "hid"));
        } catch (SQLException e) {
            cl.setHid(null);
        }
        try {
            cl.setCid(rs.getLong(su + "cid"));
        } catch (SQLException e) {
            cl.setCid(null);
        }
        try {
            cl.setTid(rs.getLong(su + "tid"));
        } catch (SQLException e) {
            cl.setTid(null);
        }
        try {
            cl.setTitle(rs.getString(su + "title"));
        } catch (SQLException e) {
            cl.setTitle(null);
        }

        try {
            cl.setContent(rs.getString(su + "content"));
        } catch (SQLException e) {
            cl.setContent(null);
        }
        
        try {
            cl.setImg(rs.getString(su + "img"));
        } catch (SQLException e) {
            cl.setImg(null);
        }
        
        try {
            cl.setCdate(rs.getTimestamp(su + "cdate"));
        } catch (SQLException e) {
            cl.setCdate(null);
        }
        try {
            cl.setTname(rs.getString(su+"tname"));
        } catch (SQLException e) {
            cl.setTname(null);
        }
        try {
            cl.setHeadimg(rs.getString(su+"headimg"));
        } catch (SQLException e) {
            cl.setHeadimg(null);
        }
        try {
            cl.setCount(rs.getInt(su+"count"));
        } catch (SQLException e) {
            cl.setCount(null);
        }
        try {
            cl.setReading(rs.getInt(su+"reading"));
        } catch (SQLException e) {
            cl.setReading(null);
        }
        return cl;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, Homework cl) {
        par.put("hid", cl.getHid());
        par.put("cid", cl.getCid());
        par.put("tid", cl.getTid());
        par.put("title", cl.getTitle());
        par.put("content", cl.getContent());
        par.put("img", cl.getImg());
        par.put("cdate", cl.getCdate());
        par.put("tname",cl.getTname());
        par.put("headimg", cl.getHeadimg());
        par.put("count", cl.getCount());
        par.put("reading", cl.getReading());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            Homework cl) {
        args.put("hid", cl.getHid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final Homework cl) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(cl),
                        new String[] { "hid" });
                parsePS(ps, cl);
                return ps;
            }

        };
    }

    private static String parseSQL(Homework cl) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into homework(");
        int count = 0;
        if (null != cl.getCid()) {
            sql.append("cid,");
            count++;
        }

        if (null != cl.getTid()) {
            sql.append("tid,");
            count++;
        }

        if (null != cl.getTitle()) {
            sql.append("title,");
            count++;
        }
        
        if (null != cl.getContent()) {
            sql.append("content,");
            count++;
        }
        
        if (null != cl.getImg()) {
            sql.append("img,");
            count++;
        }
        
        if (null != cl.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        
        if (null != cl.getCount()) {
            sql.append("count,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, Homework cl)
        throws SQLException {
        int count = 1;
        if (null != cl.getCid()) {
            ps.setLong(count++, cl.getCid());
        }

        if (null != cl.getTid()) {
            ps.setLong(count++, cl.getTid());
        }

        if (null != cl.getTitle()) {
            ps.setString(count++, cl.getTitle());
        }
        
        if (null != cl.getContent()) {
            ps.setString(count++, cl.getContent());
        }
        
        if (null != cl.getImg()) {
            ps.setString(count++, cl.getImg());
        }
        
        if (null != cl.getCdate()) {
            ps.setTimestamp(count++, cl.getCdate());
        }
        
        if (null != cl.getCount()) {
            ps.setInt(count++, cl.getCount());
        }

    }

}
